<?php
/* * **************************************************************************************\
 * *   @name		EXP Autos  2.0                                                  **
 * *   @package          Joomla 1.6                                                      **
 * *   @author		EXP TEAM::Alexey Kurguz (Grusha)                                **
 * *   @copyright	Copyright (C) 2005 - 2011  EXP TEAM::Alexey Kurguz (Grusha)     **
 * *   @link             http://www.feellove.eu                                          **
 * *   @license		Commercial License                                              **
  \*************************************************************************************** */

// No direct access.
defined('_JEXEC') or die;

$params_file = JPATH_COMPONENT . '/skins/expimages/default_jquery/parameters/params.php';
if(file_exists($params_file))
require_once $params_file;
ExpAutosProHelper::expskin_lang('expimages','default_jquery');

$expitem = $this->expitemid;
$cache = JFactory::getCache('com_expautospro', '');
$cache->clean('com_expautospro');
$user = JFactory::getUser();
$document = JFactory::getDocument();
$document->addStyleSheet(JURI::root() . 'components/com_expautospro/assets/css/expautospro.css');
$document->addStyleSheet(JURI::root() . 'components/com_expautospro/skins/expimages/default_jquery/css/default.css');
$document->addScript(JURI::root() . 'components/com_expautospro/assets/js/ajaxrequest.js');

/* Version 3.5 */
if($expuse_jquery)
    $document->addScript(JURI::root() . 'components/com_expautospro/assets/js/jquery.min.js');
$document->addScript(JURI::root() . 'components/com_expautospro/skins/expimages/default_jquery/js/jquery.ui.widget.js');
$document->addScript(JURI::root() . 'components/com_expautospro/skins/expimages/default_jquery/js/jquery.iframe-transport.js');
$document->addScript(JURI::root() . 'components/com_expautospro/skins/expimages/default_jquery/js/jquery.fileupload.js');
//$document->addScript(JURI::root() . 'components/com_expautospro/skins/expimages/default_jquery/js/jquery.fileupload-ui.js');
$topmoduleposition = $this->expparams->get('c_admanager_addimages_tmpname');
$bmoduleposition = $this->expparams->get('c_admanager_addimages_bmpname');
$addid = (int) JRequest::getInt('id', null, '', 'array');
$checkaduser = ExpAutosProFields::expaddcheck((int) $addid, (int) $user->id);
$listimages = $this->expimages;
$countimages = count($this->expimages);
$imgcount = ExpAutosProHelper::getExpcount('images', 'catid', $addid);
$groupimg = $this->expgroupfields->get('g_imgcount');
$groupimgcount = $groupimg ? $groupimg : '5';
$imgcount = $groupimgcount - $countimages;

$expisnew = (int) JRequest::getInt('expisnew', '');
//print_r($this->expgroupfields->get('g_imgcount'));
//print_r($this->form->getValue('catid'));
//print_r($this->expgroupfields->get('g_adscount'));
//print_r($ads_count);
?>

<script>
    jQuery(function () {
        jQuery('.checkall').click(function () {
            jQuery(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
        });
        jQuery('#expdeleteimg').click(function () {
            datapost = jQuery('input:checkbox').serialize();
            jQuery('#expimginfo').empty();
            if(datapost.length > 0){
                jQuery.ajax({
                    url: "index.php?option=com_expautospro&view=expimages&format=ajax&delid=1&catid=<?php echo $addid; ?>",
                    data: datapost,
                    type: "POST",
                    dataType: 'json',
                    beforeSend : function (xhr) {
                        jQuery('#exploader').addClass('loading');  
                    },
                    complete : function (xhr) {
                        jQuery('#exploader').removeClass('loading'); 
                    },
                    success: function(data) {
                        jQuery.each(data, function (index, file) {
                            var maxcount = <?php echo $groupimgcount; ?> - file.imgcount;
                            jQuery('#expmaxuploadimg').text(maxcount);
                            if(maxcount > 0)
                                jQuery('#expformimg').css('display', 'block');
                            jQuery('#expimg-'+this.imgid).remove();
                        });
                    }
                });
            }else{
                jQuery('#expimginfo').append('<p class="warning"><?php echo JText::_('COM_EXPAUTOSPRO_IMAGES_DELETESELECT_TEXT'); ?></p>');
            }
        });
     
        jQuery('#expfileupload').fileupload({
            dataType: 'json',
            url: 'index.php?option=com_expautospro&view=expimages&format=ajax&catid=<?php echo $addid; ?>',
            submit:function (e, data) {
                var maxnum = jQuery('#expmaxuploadimg').html();
                if(maxnum <= 0)
                    return false;
            },
            start: function (e) {
                jQuery('#exploader').addClass('loading');
                jQuery('#expimginfo').empty();
            },
            stop: function (e) {
                jQuery('#exploader').removeClass('loading');
            },
            done: function (e, data) {
                jQuery.each(data.result, function (index, file) {
                    if(file.text){
                        jQuery('#expimginfo').append(file.text);
                    }else{
                        var maxcount = <?php echo $groupimgcount; ?> - file.imgcount;
                        jQuery('#expmaxuploadimg').text(maxcount);
                        if(maxcount == 0)
                            jQuery('#expformimg').css('display', 'none');
                        var html = '\
                    <td>\
                        <input type="checkbox" name="del_image['+file.imgid+']"  value="'+file.imgname+'">\
                    </td>\
                    <td>\
                        <img src="<?php echo ExpAutosProExpparams::ImgUrlPatchThumbs(); ?>'+file.imgname+'"/>\
                    </td>\
                                <td>\
                                    <textarea name="imgdescription['+file.imgid+']" id="" rows="3" cols="30"  ></textarea>\
                                </td>\
                                <td>\
                                    <input class="inputbox" type="text" name="imgordering['+file.imgid+']" id="" size="2" maxlength="2" value="'+file.imgcount+'" />\
                                </td>\
                    ';
                                                            jQuery('<tr/>').attr('id','expimg-'+file.imgid).html(html).prependTo('.expimagestable');
                                                        }
                                                    });
                                                }
                                            });
                                        });
</script>
<div class="expautospro_topmodule">
    <div class="expautospro_topmodule_pos">
        <?php echo ExpAutosProHelper::load_module_position($topmoduleposition, $this->expparams->get('c_admanager_addimages_tmpstyle')); ?>
    </div>
    <div class="expautospro_clear"></div>
</div>

<!-- Skins Module Position !-->
<?php if($this->expparams->get('c_admanager_addimages_showskin')):?>
<div id="expskins_module">
    <?php
    $expmodparam = array('folder' => $this->expskins);
    echo ExpAutosProHelper::load_module_position('expskins', $style = 'none', $expmodparam);
    ?>
</div>
<div class="expautospro_clear"></div>
<?php endif; ?>
<div id="expautospro">
    <h2><?php echo JText::_('COM_EXPAUTOSPRO_CP_ADDIMAGES_TEXT') ?></h2>
    <?php if ($checkaduser): ?>
        <div class="expautos_imgconfig">
            <p>
                <?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGES_MINWIDTH_TEXT') . $this->expparams->get('c_images_minsize_width') . JText::_('COM_EXPAUTOSPRO_CP_PX_TEXT'); ?>
            </p>
            <p>
                <?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGES_MINHEIGHT_TEXT') . $this->expparams->get('c_images_minsize_height') . JText::_('COM_EXPAUTOSPRO_CP_PX_TEXT'); ?>
            </p>
            <p>
                <?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGES_MAXFILESIZE_TEXT') . ExpAutosProFields::exp_convertsize($this->expparams->get('c_images_maxfilesize')); ?>
            </p>
            <p>
                <?php echo JText::_('COM_EXPAUTOSPRO_IMAGES_MAXNUMUPLOAD_TEXT'); ?>
                <span id="expmaxuploadimg"><?php echo $imgcount; ?></span>
            </p>
            <p>
                <?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGES_OTHERS_TEXT'); ?>
            </p>
        </div>

        <div id="expformimg" <?php if ($imgcount <= 0): ?>style="display: none;"<?php endif; ?>>
            <fieldset>
                <legend><strong><?php echo JText::_('COM_EXPAUTOSPRO_IMAGES_SELECTIMG_TEXT'); ?></strong></legend>
            <input id="expfileupload" type="file" name="adimages[]" multiple />
            </fieldset>
        </div>
        <div class="expprofile-edit">
            <form id="expmember-profile" action="<?php echo JRoute::_('index.php?option=com_expautospro&view=expimages&task=expimages'); ?>" method="post" enctype="multipart/form-data" name="adminForm" id="expautosprocat-form" class="form-validate">
                <fieldset>

                    <div id="expimginfo"></div>
                    <div id="exploader"></div>
                    <div class="expimgbutton">
                        <input id="expdeleteimg" type="button" class="button" value="<?php echo JText::_('COM_EXPAUTOSPRO_IMAGES_DELETESELECTED_TEXT'); ?>"/>
                    </div> 

                    <legend><?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGES_TEXT'); ?></legend>

                    <table class="expimagestable">
                        <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" class="checkall" value="0"/> <?php echo JText::_('COM_EXPAUTOSPRO_CP_LIST_TABLE_DELETE_TEXT'); ?>
                                </th>
                                <th>
                                    <?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGE_TEXT'); ?>
                                </th>
                                <th>
                                    <?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGE_DESCRIPTION_TEXT'); ?>
                                </th>
                                <th>
                                    <?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGE_ORDERING_TEXT'); ?>
                                </th>
                            </tr>
                        </thead>
                        <?php for ($i = 0, $n = $countimages; $i < $n; $i++) { ?>
                            <tr id="expimg-<?php echo $listimages[$i]->id; ?>">
                                <td>
                                    <input type='checkbox' name='del_image[<?php echo $listimages[$i]->id; ?>]' value='<?php echo $listimages[$i]->name; ?>'>
                                </td>
                                <td>
                                    <?php
                                    $img_urlpatch = ExpAutosProExpparams::ImgUrlPatchThumbs() . $listimages[$i]->name;
                                    $img_abspatch = ExpAutosProExpparams::ImgAbsPathThumbs() . $listimages[$i]->name;
                                    if (file_exists($img_abspatch)) {
                                        echo '<img src="' . $img_urlpatch . '"/>';
                                    }
                                    ?>
                                </td>
                                <td>
                                    <textarea name="imgdescription[<?php echo $listimages[$i]->id; ?>]" id="jform_description<?php echo $i; ?>" rows="3" cols="30"  ><?php echo $listimages[$i]->description ?></textarea>
                                </td>
                                <td>
                                    <input class="inputbox" type="text" name="imgordering[<?php echo $listimages[$i]->id; ?>]" id="jform_ordering<?php echo $i; ?>" size="2" maxlength="2" value="<?php echo $listimages[$i]->ordering ?>" />
                                </td>
                            </tr>
                            <?php
                        }
                        ?>
                    </table>
                </fieldset>
                <div>
                    <button type="submit" class="validate"><span><?php echo JText::_('COM_EXPAUTOSPRO_CP_IMAGE_SAVEANDFINISHED_TEXT'); ?></span></button>

                    <input type="hidden" name="option" value="com_expautospro" />
                    <input type="hidden" name="catid" value="<?php echo $addid; ?>" />
                    <input type="hidden" name="task" value="expimages.save" />
                    <?php if($expisnew): ?>
                        <input type="hidden" name="expisnew" value="1" />
                    <?php endif; ?>
                    <input type="hidden" name="Itemid" value="<?php echo $expitem; ?>" />
                    <?php echo JHtml::_('form.token'); ?>
                </div>
            </form>
        </div>
    <?php else: ?>
        <?php echo JText::_('COM_EXPAUTOSPRO_CP_EXPADD_NOTUSERAD_ERROR_TEXT'); ?>
    <?php endif; ?>

</div>
<div class="expautospro_clear"></div>
<?php if ($bmoduleposition): ?>
    <div class="expautospro_botmodule">
        <?php echo ExpAutosProHelper::load_module_position($bmoduleposition, $this->expparams->get('c_admanager_addimages_bmpstyle')); ?>
    </div>
<?php endif; ?>
